Getting Started with Inmetpy¶

In [32]:
import plotly.io as pio
pio.renderers.default = "plotly_mimetype+notebook"

Welcome! The INMetPy provides some straight methods to obtain details and data from all meteorological stations of the Brazilian National Institute of Meteoroly, INMET. Some examples of how to use the INMetPy API are showed below.

Loading package¶

To load the package:

In [1]:
from inmetpy.stations import InmetStation

inmet = InmetStation()

Stations¶

The first thing to know about the meteorological stations is their Code ID. These ids will be used in other methods to query the meteorological data. The method get_stations shows the details of each station.

In [2]:
stations = inmet.get_stations()
stations.head(10)
Out[2]:
CD_OSCAR STATION_NAME CD_STATION TP_STATION START_DATE_OPERATION END_DATE_OPERATION LONGITUDE LATITUDE HEIGHT CD_SITUATION CD_WSI IS_CAPITAL CD_DISTRICT INSTITUTE STATE
0 0-2000-0-86765 ABROLHOS A422 Automatic 2008-07-21 00:00:00+00:00 NaT -38.703333 -17.963056 20.93 Down 0-76-0-2906907000000408 False 04 INMET BA
1 0-2000-0-81755 ACARAU A360 Automatic 2009-04-22 00:00:00+00:00 NaT -40.087222 -3.121111 67.15 Operative 0-76-0-2300200000000446 False 03 INMET CE
2 0-2000-0-86827 AFONSO CLAUDIO A657 Automatic 2011-09-24 00:00:00+00:00 NaT -41.106944 -20.104167 507.48 Operative 0-76-0-3200102000000478 False 06 INMET ES
3 0-2000-0-86686 AGUA BOA A908 Automatic 2006-12-16 00:00:00+00:00 NaT -52.211667 -14.016389 440.00 Operative 0-76-0-5100201000000157 False 09 INMET MT
4 0-2000-0-86812 AGUA CLARA A756 Automatic 2010-08-14 00:00:00+00:00 NaT -52.875833 -20.444444 323.63 Operative 0-76-0-5000203000000463 False 07 INMET MS
5 0-2000-0-86716 AGUAS EMENDADAS A045 Automatic 2008-10-03 00:00:00+00:00 NaT -47.625801 -15.596491 1030.36 Operative 0-76-0-5300108000000435 False 10 INMET DF
6 0-2000-0-86722 AGUAS VERMELHAS A549 Automatic 2007-09-09 00:00:00+00:00 NaT -41.457787 -15.751536 754.07 Operative 0-76-0-3101003000000252 False 05 INMET MG
7 0-2000-0-86803 AIMORES A534 Automatic 2007-08-05 00:00:00+00:00 NaT -41.090833 -19.532778 287.74 Operative 0-76-0-3101102000000239 False 05 INMET MG
8 0-2000-0-86828 ALEGRE A617 Automatic 2006-10-25 00:00:00+00:00 NaT -41.488889 -20.750556 138.00 Operative 0-76-0-3200201000000125 False 06 INMET ES
9 0-2000-0-86975 ALEGRETE A826 Automatic 2006-09-28 00:00:00+00:00 NaT -55.525556 -29.709167 120.88 Operative 0-76-0-4300406000000113 False 08 INMET RS

Above is showed all the meteorological stations that are avaible on INMET API. These includes either automatic stations and manual stations. To filter in the query, use type to indicate which type of station is searched. For automatic, uses type = "A", for manual type = "M". The default value is type = "ALL", which shows both types.

In [4]:
automatic_stations = inmet.get_stations(type="A")
automatic_stations.head(5)
Out[4]:
CD_OSCAR STATION_NAME CD_STATION TP_STATION START_DATE_OPERATION END_DATE_OPERATION LONGITUDE LATITUDE HEIGHT CD_SITUATION CD_WSI IS_CAPITAL CD_DISTRICT INSTITUTE STATE
0 0-2000-0-86765 ABROLHOS A422 Automatic 2008-07-21 00:00:00+00:00 NaT -38.703333 -17.963056 20.93 Down 0-76-0-2906907000000408 False 04 INMET BA
1 0-2000-0-81755 ACARAU A360 Automatic 2009-04-22 00:00:00+00:00 NaT -40.087222 -3.121111 67.15 Operative 0-76-0-2300200000000446 False 03 INMET CE
2 0-2000-0-86827 AFONSO CLAUDIO A657 Automatic 2011-09-24 00:00:00+00:00 NaT -41.106944 -20.104167 507.48 Operative 0-76-0-3200102000000478 False 06 INMET ES
3 0-2000-0-86686 AGUA BOA A908 Automatic 2006-12-16 00:00:00+00:00 NaT -52.211667 -14.016389 440.00 Operative 0-76-0-5100201000000157 False 09 INMET MT
4 0-2000-0-86812 AGUA CLARA A756 Automatic 2010-08-14 00:00:00+00:00 NaT -52.875833 -20.444444 323.63 Operative 0-76-0-5000203000000463 False 07 INMET MS
In [6]:
manual_stations = inmet.get_stations(type="M")
manual_stations.head(5)
Out[6]:
CD_OSCAR STATION_NAME CD_STATION TP_STATION START_DATE_OPERATION END_DATE_OPERATION LONGITUDE LATITUDE HEIGHT CD_SITUATION CD_WSI IS_CAPITAL CD_DISTRICT INSTITUTE STATE
573 0-2000-0-82989 AGUA BRANCA 82989 Traditional 1928-05-05 00:00:00+00:00 NaT -37.938056 -9.264722 603.42 Operative 0-76-0-2700102000W82989 False 03 INMET AL
574 0-2000-0-82353 ALTAMIRA 82353 Traditional 1927-04-24 00:00:00+00:00 NaT -52.213056 -3.214167 101.51 Operative 0-76-0-1500602000W82353 False 02 INMET PA
575 0-2000-0-83007 ALTO DA BOA VISTA 83007 Traditional 1966-06-01 00:00:00+00:00 NaT -43.279167 -22.965833 347.10 Operative 0-76-0-3304557000W83007 False 06 INMET RJ
576 0-2000-0-82590 APODI 82590 Traditional 1962-12-18 00:00:00+00:00 NaT -37.815000 -5.626667 131.37 Operative 0-76-0-2401008000W82590 False 03 INMET RN
577 0-2000-0-83442 ARACUAI 83442 Traditional 1918-09-06 00:00:00+00:00 NaT -42.060833 -16.848889 317.67 Operative 0-76-0-3103405000W83442 False 05 INMET MG

It is also possible to search stations for specific states of Brazil using search_station_by_state.

In [8]:
states = ['PE','BA','SE','PI']
stations = inmet.search_station_by_state(st=states)
stations.head(5)
Out[8]:
CD_OSCAR STATION_NAME CD_STATION TP_STATION START_DATE_OPERATION END_DATE_OPERATION LONGITUDE LATITUDE HEIGHT CD_SITUATION CD_WSI IS_CAPITAL CD_DISTRICT INSTITUTE STATE
0 0-2000-0-86765 ABROLHOS A422 Automatic 2008-07-21 00:00:00+00:00 NaT -38.703333 -17.963056 20.93 Down 0-76-0-2906907000000408 False 04 INMET BA
19 0-2000-0-81846 ALVORADA DO GURGUEIA A336 Automatic 2007-11-17 00:00:00+00:00 NaT -43.865556 -8.441667 261.26 Operative 0-76-0-2202307000000274 False 03 INMET PI
21 0-2000-0-86675 AMARGOSA A434 Automatic 2008-07-11 00:00:00+00:00 NaT -39.616944 -13.009444 397.80 Down 0-76-0-2901007000000398 False 04 INMET BA
22 0-2000-0-81867 ANGICAL DO PIAUI A377 Automatic 2019-11-14 00:00:00+00:00 NaT -42.727500 -6.089444 183.00 Operative 0-76-0-2200608000000597 False 03 INMET PI
28 0-2000-0-86616 ARACAJU A409 Automatic 2003-02-12 00:00:00+00:00 NaT -37.054444 -10.952500 3.72 Down 0-76-0-2800308000000055 False 04 INMET SE

To search for a specific type of station, use the type argument.

In [10]:
manual_stations = inmet.search_station_by_state(st=states, station_type="M")
manual_stations.tail(5)
Out[10]:
CD_OSCAR STATION_NAME CD_STATION TP_STATION START_DATE_OPERATION END_DATE_OPERATION LONGITUDE LATITUDE HEIGHT CD_SITUATION CD_WSI IS_CAPITAL CD_DISTRICT INSTITUTE STATE
709 0-2000-0-82879 SAO JOAO DO PIAUI 82879 Traditional 1975-09-30 00:00:00+00:00 NaT -42.250278 -8.365000 237.02 Operative 0-76-0-2210003000W82879 False 03 INMET PI
717 0-2000-0-83190 SERRINHA 83190 Traditional 1904-01-31 23:59:56+00:00 NaT -39.001944 -11.668056 362.39 Operative 0-76-0-2930501000W83190 False 04 INMET BA
719 0-2000-0-82797 SURUBIM 82797 Traditional 1929-10-01 00:00:00+00:00 NaT -35.801056 -7.839628 421.44 Operative 0-76-0-2614501000W82797 False 03 INMET PE
723 0-2000-0-82578 TERESINA 82578 Traditional 1911-03-20 23:59:56+00:00 NaT -42.801389 -5.034722 75.73 Operative 0-76-0-2211001000W82578 True 03 INMET PI
730 0-2000-0-83344 VITORIA DA CONQUISTA 83344 Traditional 1936-01-01 00:00:00+00:00 NaT -40.801389 -14.886389 879.47 Operative 0-76-0-2933307000W83344 False 04 INMET BA

Search stations by coordinates¶

If you want to search stations that are near by a specific location, you can use the search_station_by_coords method, using the coordinates of the central point searched. It can be specified the number of stations that will return using n_stations (default = 1).

In [11]:
stations_nearby = inmet.search_station_by_coords(lat=-22.42, lon=-24.42, n_stations = 5)
stations_nearby
Out[11]:
CD_OSCAR STATION_NAME CD_STATION TP_STATION START_DATE_OPERATION END_DATE_OPERATION LONGITUDE LATITUDE HEIGHT CD_SITUATION CD_WSI IS_CAPITAL CD_DISTRICT INSTITUTE STATE DISTANCE
0 0-2000-0-86765 ABROLHOS A422 Automatic 2008-07-21 00:00:00+00:00 NaT -38.703333 -17.963056 20.93 Down 0-76-0-2906907000000408 False 04 INMET BA 1569.912307
612 0-2000-0-83498 CARAVELAS 83498 Traditional 1930-05-01 00:00:00+00:00 NaT -39.258611 -17.739444 6.05 Operative 0-76-0-2906907000W83498 False 04 INMET BA 1633.686893
122 0-2000-0-86764 CARAVELAS A405 Automatic 2002-12-20 00:00:00+00:00 NaT -39.258611 -17.739444 6.05 Down 0-76-0-2906907000000039 False 04 INMET BA 1633.686893
290 0-2000-0-86805 LINHARES A614 Automatic 2006-10-27 00:00:00+00:00 NaT -40.068611 -19.356944 40.00 Operative 0-76-0-3203205000000128 False 06 INMET ES 1660.074084
488 0-2000-0-86786 SAO MATEUS A616 Automatic 2006-10-25 00:00:00+00:00 NaT -39.864167 -18.676111 28.66 Operative 0-76-0-3204906000000124 False 06 INMET ES 1660.088788
In [33]:
import pandas as pd 
import plotly.express as px 


from inmetpy.inmet_stations import InmetStation
In [34]:
inmet = InmetStation()

stations = inmet.stations
In [35]:
stations.LATITUDE = stations.LATITUDE.astype(float)
stations.LONGITUDE = stations.LONGITUDE.astype(float)

Get details of all stations¶

In [36]:
fig = px.scatter_mapbox(stations, lat="LATITUDE", lon="LONGITUDE", hover_name="STATION_NAME", hover_data=["TP_STATION", "CD_SITUATION"],
                        color = "CD_SITUATION", 
                        zoom=3.8, height=300, 
                        color_discrete_map= {'Down': 'red',
                                             'Operative': 'green'})
                                                                  
fig.update_layout(mapbox_style="open-street-map")
fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0})
fig.update_layout(height=800, width=1000)
fig.update_layout(legend=dict(
    yanchor="top",
    y=0.99,
    xanchor="left",
    x=0.79
))
fig.show()
In [37]:
# Mapa com a coloração da temperatura
all_stations_data = inmet.get_all_stations()
In [38]:
all_stations_data.LAT
Out[38]:
0        -15.78944444
1        -15.78944444
2        -15.78944444
3        -15.78944444
4        -15.78944444
             ...     
14396    -19.97999999
14397    -19.97999999
14398    -19.97999999
14399    -19.97999999
14400    -19.97999999
Name: LAT, Length: 14401, dtype: object
In [39]:
all_stations_data.LAT = all_stations_data.LAT.astype(float)
all_stations_data.LONG = all_stations_data.LONG.astype(float)
In [40]:
all_stations_data.TEMP = all_stations_data.TEMP.astype(float)
In [41]:
daily_mean = all_stations_data.groupby(['STATION_NAME', 'LAT','LONG'])['TEMP'].mean().to_frame(name = 'TEMP').reset_index()
In [42]:
daily_mean.dropna(inplace=True)
In [43]:
daily_mean
Out[43]:
STATION_NAME LAT LONG TEMP
1 ACARAÚ -3.121111 -40.087222 25.962500
2 AFONSO CLAUDIO -20.104167 -41.106944 20.370833
3 AGUAS EMENDADAS -15.596491 -47.625801 19.425000
4 AIMORES -19.532778 -41.090833 25.057143
5 ALEGRE -20.750556 -41.488889 21.900000
... ... ... ... ...
592 VIÇOSA -20.762607 -42.864013 16.782609
596 ZE DOCA -3.269167 -45.651111 27.329167
598 ÁGUA CLARA -20.444444 -52.875833 24.375000
599 ÁGUAS VERMELHAS -15.751536 -41.457787 20.375000
600 ÓBIDOS -1.880833 -55.519722 27.087500

421 rows × 4 columns

In [ ]:
 
In [44]:
fig = px.scatter_mapbox(daily_mean, lat="LAT", lon="LONG", color="TEMP",
                  color_continuous_scale=px.colors.cyclical.IceFire, zoom=3.8,
                       mapbox_style = 'open-street-map')
fig.update_layout(mapbox_style="open-street-map")
fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0})
fig.update_layout(height=800, width=1000)
fig.show()
In [45]:
states = ['RJ']
rj_stations = inmet.search_station_by_state(states, station_type = "A")
In [46]:
rj_stations
Out[46]:
CD_OSCAR STATION_NAME FL_CAPITAL END_DATE_OPERATION CD_SITUATION TP_STATION LATITUDE CD_WSI CD_DISTRICT HEIGHT STATE INSTITUTE CD_STATION LONGITUDE START_DATE_OPERATION
23 None ANGRA DOS REIS N None Operative Automatic -22.975556 0-76-0-3300100000000555 06 6 RJ INMET A628 -44.303333 2017-08-24T21:00:00.000-03:00
41 0-2000-0-86892 ARRAIAL DO CABO N None Operative Automatic -22.975278 0-76-0-3300258000000108 06 5 RJ INMET A606 -42.021389 2006-09-21T21:00:00.000-03:00
98 0-2000-0-86854 CAMBUCI N None Operative Automatic -21.587500 0-76-0-3300902000000025 06 46 RJ INMET A604 -41.958333 2002-11-19T22:00:00.000-02:00
109 0-2000-0-86855 CAMPOS DOS GOYTACAZES N None Operative Automatic -21.714722 0-76-0-3301009000000109 06 17 RJ INMET A607 -41.343889 2006-09-24T21:00:00.000-03:00
110 0-2000-0-86890 CAMPOS DOS GOYTACAZES - SAO TOME N None Operative Automatic -22.041667 0-76-0-3301009000000382 06 7 RJ INMET A620 -41.051667 2008-06-12T21:00:00.000-03:00
125 None CARMO N None Operative Automatic -21.938745 0-76-0-3301207000000583 06 293 RJ INMET A629 -42.600936 2018-10-10T21:00:00.000-03:00
180 0-2000-0-86877 DUQUE DE CAXIAS - XEREM N None Operative Automatic -22.589722 0-76-0-3301702000000022 06 22 RJ INMET A603 -43.282222 2002-10-20T21:00:00.000-03:00
255 None ITATIAIA - AGULHAS NEGRAS N None Operative Automatic -22.373889 0-76-0-3302254000000557 06 2450 RJ INMET A635 -44.703056 2017-08-31T21:00:00.000-03:00
295 0-2000-0-86891 MACAE N None Operative Automatic -22.376111 0-76-0-3302403000000106 06 28 RJ INMET A608 -41.811944 2006-09-21T21:00:00.000-03:00
338 None NITEROI N None Operative Automatic -22.867500 0-76-0-3303302000000580 06 6 RJ INMET A627 -43.101944 2018-07-12T21:00:00.000-03:00
341 0-2000-0-86889 NOVA FRIBURGO - SALINAS None None Operative Automatic -22.334722 0-76-0-3303401000000466 06 1070 RJ INMET A624 -42.676944 2010-09-17T21:00:00.000-03:00
370 0-2000-0-86913 PARATY N None Operative Automatic -23.223611 0-76-0-3303807000000141 06 3 RJ INMET A619 -44.726944 2006-11-18T21:00:00.000-03:00
386 0-2000-0-86876 PICO DO COUTO N None Operative Automatic -22.464722 0-76-0-3303906000000121 06 1777 RJ INMET A610 -43.291389 2006-10-21T21:00:00.000-03:00
428 0-2000-0-86874 RESENDE N None Operative Automatic -22.451389 0-76-0-3304201000000114 06 438.83 RJ INMET A609 -44.445000 2006-09-28T21:00:00.000-03:00
432 None RIO CLARO N None Operative Automatic -22.653579 0-76-0-3304409000000518 06 516 RJ INMET A626 -44.040916 2016-06-02T21:00:00.000-03:00
433 0-2000-0-86887 RIO DE JANEIRO - FORTE DE COPACABANA None None Operative Automatic -22.988333 0-76-0-3304557000000187 06 25.59 RJ INMET A652 -43.190556 2007-05-17T21:00:00.000-03:00
434 None RIO DE JANEIRO - JACAREPAGUA N None Operative Automatic -22.940000 0-76-0-3304557000000553 06 20 RJ INMET A636 -43.402778 2017-08-09T21:00:00.000-03:00
435 0-2000-0-86879 RIO DE JANEIRO - VILA MILITAR N None Operative Automatic -22.861389 0-76-0-3304557000000179 06 30.43 RJ INMET A621 -43.411389 2007-04-12T21:00:00.000-03:00
436 0-2000-0-86914 RIO DE JANEIRO-MARAMBAIA N None Operative Automatic -23.050278 0-76-0-3304557000000024 06 12 RJ INMET A602 -43.595556 2002-11-07T22:00:00.000-02:00
461 None SANTA MARIA MADALENA N None Operative Automatic -21.950556 0-76-0-3304607000000584 06 517 RJ INMET A630 -42.010278 2018-10-15T21:00:00.000-03:00
503 0-2000-0-86885 SAQUAREMA - SAMPAIO CORREIA N None Operative Automatic -22.871111 0-76-0-3305505000000507 06 26 RJ INMET A667 -42.608889 2015-09-01T21:00:00.000-03:00
506 0-2000-0-86878 SEROPEDICA-ECOLOGIA AGRICOLA N None Operative Automatic -22.757778 0-76-0-3305554000000002 06 35 RJ INMET A601 -43.684722 2000-05-23T21:00:00.000-03:00
515 0-2000-0-86893 SILVA JARDIM N None Operative Automatic -22.645833 0-76-0-3305604000000505 06 19 RJ INMET A659 -42.415556 2015-08-27T21:00:00.000-03:00
531 0-2000-0-86888 TERESOPOLIS-PARQUE NACIONAL N None Operative Automatic -22.448611 0-76-0-3305802000000131 06 981 RJ INMET A618 -42.986944 2006-10-31T21:00:00.000-03:00
540 None TRES RIOS N None Operative Automatic -22.098333 0-76-0-3306008000000519 06 295 RJ INMET A625 -43.208333 2016-06-07T21:00:00.000-03:00
556 0-2000-0-86875 VALENCA N None Operative Automatic -22.358056 0-76-0-3306107000000111 06 370 RJ INMET A611 -43.695556 2006-09-26T21:00:00.000-03:00
In [47]:
stations = ['A627']
niteroi = inmet.get_data_station('2019-01-01','2020-01-01', by='day',station_id = stations)
A627
Looking for station A627...
In [48]:
niteroi.TEMP_MED.plot(x='DATE')
Out[48]:
<AxesSubplot:>
In [49]:
niteroi.columns
Out[49]:
Index(['index', 'DATE', 'AVG_RH', 'STATION_NAME', 'MIN_RH', 'TEMP_MED', 'RAIN',
       'LAT', 'TEMP_MIN', 'TEMP_MAX', 'ST', 'WSPD', 'STATION_ID', 'LONG'],
      dtype='object')
In [50]:
temp = px.line(x=niteroi.DATE,y=niteroi.TEMP_MED, labels = {'x':'Date', 'y':'°C'},
              markers='line+markers')
temp.update_layout(title="Niteroi Station Temperature")
temp.show()
In [ ]: